import {Page} from '@/base/Page';
import {Model} from '@/base/model/Model';
import {getProductModelList} from '@/base/api/LvApi';
import axios from 'axios';

export class ProductSelectorBloc {

  paged = {items: []};
  loading = false;
  timer: number | undefined;
  // cancelsource = axios.CancelToken.source();

  load(name: string) {
    this.loading = true;
    if (this.timer) {
        clearTimeout(this.timer);
    }
    if (name) {
      this.timer = setTimeout(() => {
          return getProductModelList({
            productName: name
          }).then((res) => {
            // @ts-ignore
            this.paged.items = res.data;
          }).finally(() => {
            this.loading = false;
          });
      }, 300);
    } else {
      this.paged = {items: []};
    }
  }
}
